package com.assistant.tasks.controller;

import com.assistant.common.Constants;
import com.assistant.common.PageResult;
import com.assistant.common.Result;
import com.assistant.common.exception.TaskNotExistsException;
import com.assistant.common.exception.file.InvalidExtensionException;
import com.assistant.tasks.pojo.TaskDTO;
import com.assistant.tasks.service.TasksService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname TasksController
 * @Description TODO
 * @Date 2025/3/4 17:35
 * @Created by xxx
 */
@RestController
@RequestMapping("/api/v1/tasks")
@Slf4j
public class TasksController {
    @Autowired
    private TasksService tasksService;

    @PostMapping("/list")
    public Result<PageResult> getTasks(@RequestBody TaskDTO taskDTO) {
        log.info("getTasks: taskDTO={}", taskDTO);
        PageResult result = tasksService.getTasks(taskDTO);
        return Result.success(result);
    }

    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result createTask(@RequestParam("category") Integer category,
                             @RequestParam("date") String date,
                             @RequestParam("containsPcdn") Integer containsPcdn,
                             @RequestParam("file") MultipartFile file) {
        TaskDTO taskDTO = new TaskDTO();
        taskDTO.setCategory(category);
        taskDTO.setDate(date);
        taskDTO.setContainsPcdn(containsPcdn);
        taskDTO.setFile(file);
        try {
            taskDTO.setInputStream(file.getInputStream());
        } catch (IOException e) {
            return Result.error(e.getMessage());
        }
        log.info("createTask: taskDTO={}", taskDTO);
        taskDTO.setSource(Constants.CREATE_USER);
        try {
            tasksService.createTask(taskDTO);
        } catch (InvalidExtensionException e) {
            return Result.error(Constants.ERROR_MSG_FILE);
        } catch (TaskNotExistsException ex) {
            return Result.error(ex.getMessage());
        }
        return Result.success();
    }

    @GetMapping
    public Result downloadResult(@RequestParam Integer id, HttpServletResponse response) {
        log.info("downloadResult: id={}", id);
        try {
            String downloadSuccess = tasksService.getResult(id, response);
            if (downloadSuccess!=null) {
                response.setStatus(HttpStatus.NOT_FOUND.value());
                return Result.error(downloadSuccess);
            }
        } catch (Exception e) {
            try {
                response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Download failed");
            } catch (IOException ex) {
                return Result.error(ex.getMessage());
            }
        }
        return Result.success();
    }

}
